package com.iqiyi.pps.epg.web.service.passport;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.pps.epg.core.utils.ApiHttpRequestUtil;
import com.iqiyi.pps.epg.core.utils.Constants;
import com.iqiyi.pps.epg.core.utils.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhang on 2016/1/29.
 */
@Deprecated
public class HttpPassportService {
    private static Logger logger = LoggerFactory.getLogger(HttpPassportService.class);

    private static final String HTTP_PASSPORT_URL = Configuration.getString("http.passport.url", "http://pss.qiyi.domain/services/subscribe/live_subs.htm");

    private static final Integer PASSPORT_PARAM_TYPE_UID = 1;
    private static final Integer PASSPORT_PARAM_TYPE_DEVICEID = 2;

    private static final String LIVE_SUBS_SALT = "liveE2F3K4SFGSH?#%&^sub";

    public static Map<Integer, String> getDeviceId(long episodeQipuId, int page) {
        Map<Integer, String> passportResponseMap = null;
        if (episodeQipuId <= 0 && page <= 0) {
            return passportResponseMap;
        }
        Map<String, Object> passportParams = new HashMap<String, Object>();
        passportParams.put("qipuId", episodeQipuId + "");
        passportParams.put("type", PASSPORT_PARAM_TYPE_DEVICEID + "");
        long t = System.currentTimeMillis();
        passportParams.put("t", t + "");
        StringBuffer str = new StringBuffer();
        str.append(episodeQipuId).append(PASSPORT_PARAM_TYPE_DEVICEID).append(t).append(LIVE_SUBS_SALT);
        System.out.println(str);
        String md5 = MD5Util.getMD5String(str.toString());
        passportParams.put("md5", md5);
        passportParams.put("page", page + "");

        JSONObject jsonObject = ApiHttpRequestUtil.postMethod(HTTP_PASSPORT_URL, null, passportParams, 3000);
        if (jsonObject == null) {
            return passportResponseMap;
        } else {
            String code = jsonObject.getString("code");
            if (!Constants.CODE_SUC.equals(code)) {
                String msg = jsonObject.getString("msg");
                logger.error("[HttpPassportService][getDeviceId][error][code={}][msg={}]", code, msg);
                return passportResponseMap;
            } else {
                passportResponseMap = new HashMap<Integer, String>();
                if (jsonObject.containsKey("data")) {
                    JSONObject data = null;
                    try {
                        data = jsonObject.getJSONObject("data");
                    } catch (Exception e) {

                    }
                    if (data != null) {
                        boolean hasNext = data.getBoolean("hasNext");
                        // 设置下次Page值
                        int nextPage = page;
                        if (hasNext) {
                            nextPage = page + 1;
                        } else {
                            nextPage = -1;
                        }
                        // passportResponseMap key=-1存放nextPage值
                        passportResponseMap.put(-1, nextPage + "");

                        JSONArray list = data.getJSONArray("list");
                        logger.info("[HttpPassportService][getDeviceId][episodeQipuId={}][size={}]", episodeQipuId, list.size());
                        for (int k = 0; k < list.size(); k++) {
                            String deviceId = list.getJSONObject(k).getString("deviceId");
                            Integer agentType = list.getJSONObject(k).getInteger("agentType");
                            passportResponseMap.put(k, agentType + "," + deviceId);
                        }
                    }
                }
            }
            return passportResponseMap;
        }
    }
}
